package com.soujiayi.adapter;

import java.util.ArrayList;
import java.util.List;

import com.soujiayi.activity.R;
import com.soujiayi.model.TuanMain;
import com.soujiayi.net.AsyncImageLoader;
import com.soujiayi.net.ImageLoader;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class TuanListAdapter extends BaseAdapter {
	private Activity activity;
	private ArrayList<TuanMain> tuanList;
	private AsyncImageLoader asyncImageLoader;
	private ImageLoader imageLoader;
	private int width;// 屏宽
	private int height;// 屏高

	public TuanListAdapter(Activity activity, ArrayList<TuanMain> tuanList) {
		this.activity = activity;
		this.tuanList = tuanList;
		// asyncImageLoader = new AsyncImageLoader(5);
		imageLoader = new ImageLoader(activity, true, 100, 80, 5);

		DisplayMetrics dm = new DisplayMetrics();
		dm = activity.getResources().getDisplayMetrics();
		width = (int) (100 * dm.density);
		height = (int) (80 * dm.density);
	}

	public ArrayList<TuanMain> getTuanList() {
		return tuanList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return tuanList.size();
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if (view == null) {
			view = activity.getLayoutInflater().inflate(
					R.layout.tuan_list_item, null);
			holder = new ViewHolder();
			holder.iv_tuan_goodsimage = (ImageView) view
					.findViewById(R.id.iv_tuan_goodsimage);
			holder.tv_tuan_goodstitle = (TextView) view
					.findViewById(R.id.tv_tuan_goodstitle);
			holder.tv_tuan_goodsprice = (TextView) view
					.findViewById(R.id.tv_tuan_goodsprice);
			holder.tv_tuan_goodsrebate = (TextView) view
					.findViewById(R.id.tv_tuan_goodsrebate);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		TuanMain tuanMain = tuanList.get(position);
		String title = tuanMain.getName();
		String price = tuanMain.getPrice();
		String rebate = tuanMain.getRebate();
//		if (title != null && title.length() > 30) {
//			title = title.substring(0, 30) + "...";
//		}

		if (price.indexOf(".") != -1) {
			int num = 0;
			num = price.indexOf(".");
			String dianAfter = price.substring(0, num + 1);
			String afterData = price.replace(dianAfter, "");
			if (afterData.length() < 2) {
				afterData = afterData + "0";
			}
			price = price.substring(0, num) + "." + afterData.substring(0, 2);
		} else
			price = price + ".00";

		if (rebate.indexOf(".") != -1) {
			int num = 0;
			num = rebate.indexOf(".");
			String dianAfter = rebate.substring(0, num + 1);
			String afterData = rebate.replace(dianAfter, "");
			if (afterData.length() < 2) {
				afterData = afterData + "0";
			}
			rebate = rebate.substring(0, num) + "." + afterData.substring(0, 2);
		} else
			rebate = rebate + ".00";

		holder.tv_tuan_goodstitle.setText(title == null ? "" : "["
				+ tuanList.get(position).getSitename() + "]  " + title);

		holder.tv_tuan_goodsprice.setText(price == null ? "" : "￥ " + price
				+ this.activity.getString(R.string.price_unit));
		holder.tv_tuan_goodsrebate.setText(rebate == null ? "" : this.activity
				.getString(R.string.tuan_tv_rebate) + rebate);

		// loadDrawable(tuanList.get(position).getImageUrl(),
		// holder.iv_tuan_goodsimage);

		String imageUrl = tuanList.get(position).getCover()
				.replace(".jpg", "." + width + "x" + height + ".jpg");
		imageLoader.DisplayImage(imageUrl, holder.iv_tuan_goodsimage);
		return view;

	}

	public void loadDrawable(String imageUrl, final ImageView imageView) {
		Drawable cacheImage = asyncImageLoader.loadDrawable(imageUrl,
				new AsyncImageLoader.ImageCallback() {

					public void imageLoaded(Drawable imageDrawable) {
						imageView.setImageDrawable(imageDrawable);
					}
				});

		if (cacheImage == null) {
			imageView.setImageDrawable(activity.getResources().getDrawable(
					R.drawable.pic_loading));
		} else {
			imageView.setImageDrawable(cacheImage);
		}
		if (null == imageUrl) {
			imageView.setBackgroundDrawable(activity.getResources()
					.getDrawable(R.drawable.pic_loading));
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return tuanList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	static class ViewHolder {
		TextView tv_tuan_goodstitle, tv_tuan_goodsprice, tv_tuan_goodsrebate;
		ImageView iv_tuan_goodsimage;
	}

}
